Pyqt 触发一个事件,打开外部链接,我找到了这个方法,供大家参考
1. QDesktopServices 的openUrl 方法
1 QtGui.QDesktopServices.openUrl(QtCore.QUrl('http://www.hao123.com'))
2.Python 自带的webbrowser 浏览器控制模块
webbrowser提供了三种方法
1 import webbrowser 2 webbrowser.open(url, new=0, autoraise=True)3 webbrowser.open_new(url)4 webbrowser.open_new_tab(url)
3.部分控件的Link属性链接
Pyqt有几个控件带有 setOpenExternalLinks , 如 QTextLabel 、 QTextBrowser 等
当 setOpenExternalLinks 值为TURE 表示可通过html 添加 A 标签打开外部链接, 如设置:
self.label.setOpenExternalLinks(True)
1 self.label.setText(_translate("MainWindow", "", None))
示例代码与效果:
1 # -*- coding: utf-8 -*- 2 3 # Form implementation generated from reading ui file 'URL2.ui' 4 # 5 # Created: Fri Feb 06 10:03:54 2015 6 # by: PyQt4 UI code generator 4.10.3 7 # 8 # WARNING! All changes made in this file will be lost! 9 10 from PyQt4 import QtCore, QtGui11 12 try:13 _fromUtf8 = QtCore.QString.fromUtf814 except AttributeError:15 def _fromUtf8(s):16 return s17 18 try:19 _encoding = QtGui.QApplication.UnicodeUTF820 def _translate(context, text, disambig):21 return QtGui.QApplication.translate(context, text, disambig, _encoding)22 except AttributeError:23 def _translate(context, text, disambig):24 return QtGui.QApplication.translate(context, text, disambig)25 26 class Ui_MainWindow(object):27 def setupUi(self, MainWindow):28 MainWindow.setObjectName(_fromUtf8("MainWindow"))29 MainWindow.resize(270, 313)30 self.centralwidget = QtGui.QWidget(MainWindow)31 self.centralwidget.setObjectName(_fromUtf8("centralwidget"))32 self.label = QtGui.QLabel(self.centralwidget)33 self.label.setGeometry(QtCore.QRect(80, 140, 101, 41))34 self.label.setOpenExternalLinks(True)35 self.label.setObjectName(_fromUtf8("label"))36 self.openUrl = QtGui.QPushButton(self.centralwidget)37 self.openUrl.setGeometry(QtCore.QRect(70, 40, 101, 21))38 self.openUrl.setObjectName(_fromUtf8("openUrl"))39 self.webbrowser = QtGui.QPushButton(self.centralwidget)40 self.webbrowser.setGeometry(QtCore.QRect(70, 90, 101, 21))41 self.webbrowser.setObjectName(_fromUtf8("webbrowser"))42 MainWindow.setCentralWidget(self.centralwidget)43 self.menubar = QtGui.QMenuBar(MainWindow)44 self.menubar.setGeometry(QtCore.QRect(0, 0, 270, 23))45 self.menubar.setObjectName(_fromUtf8("menubar"))46 MainWindow.setMenuBar(self.menubar)47 self.statusbar = QtGui.QStatusBar(MainWindow)48 self.statusbar.setObjectName(_fromUtf8("statusbar"))49 MainWindow.setStatusBar(self.statusbar)50 51 self.retranslateUi(MainWindow)52 QtCore.QMetaObject.connectSlotsByName(MainWindow)53 54 def retranslateUi(self, MainWindow):55 MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))56 self.label.setText(_translate("MainWindow", "lalel属性效果", None))57 self.openUrl.setText(_translate("MainWindow", "openUrl效果", None))58 self.webbrowser.setText(_translate("MainWindow", "webbrowser效果", None))59 60 61 62 63 64 class OpenUrl(QtGui.QMainWindow):65 def __init__(self):66 super(OpenUrl, self).__init__()67 self.UI=Ui_MainWindow()68 self.UI.setupUi(self)69 self.setWindowTitle(u'Pyqt打开外部链接')70 self.connect(self.UI.openUrl, QtCore.SIGNAL('clicked()'), self.openUrl)71 self.connect(self.UI.webbrowser, QtCore.SIGNAL('clicked()'), self.webbrowser)72 self.UI.label.setText(u' 腾 讯 首 页 ')73 74 def openUrl(self):75 QtGui.QDesktopServices.openUrl(QtCore.QUrl('http://www.hao123.com'))76 77 def webbrowser(self):78 import webbrowser79 webbrowser.open('http://www.sina.com.cn/')80 81 82 if __name__ == "__main__":83 import sys84 app = QtGui.QApplication(sys.argv)85 MainWindow = OpenUrl()86 MainWindow.show()87 sys.exit(app.exec_())
效果: